﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using fc2editor.DataModel;
using fc2editor.Utils;
namespace fc2editor.Lua
{
    public class LuaSerializer
    {
        public string[] Serialize(Aircraft aircraft)
        {
            if (aircraft == null)
                return new string[] { };

            var lines = new List<string>();

            if (aircraft.Pylons.Count > 0)
            {
                bool completedTillPylons = false;

                using (var fs = File.OpenRead(aircraft.FileName))
                {
                    using (var reader = new StreamReader(fs))
                    {
                        while (!reader.EndOfStream && !completedTillPylons)
                        {
                            string buffer = reader.ReadLine();
                            string trimmedBuffer = buffer.Trim();

                            while (!trimmedBuffer.StartsWith("pylon"))
                            {
                                lines.Add(buffer);

                                buffer = reader.ReadLine();
                                trimmedBuffer = buffer.Trim();
                            }

                            completedTillPylons = true;
                        }

                        lines.AddRange(SerializePylons(aircraft));
                        lines.AddRange(SerializeTasks(aircraft));
                    }
                }
            }
            else
            {
                lines.AddRange(File.ReadAllLines(aircraft.FileName));
            }

            return lines.ToArray();
        }

        public string[] SerializePylons(Aircraft aircraft)
        {
            if (aircraft == null)
                return new string[] { };

            var lines = new List<string>();

            //TODO
            //lines.Add("    {");
            foreach (var p in aircraft.Pylons.OrderBy(p => p.Index))
            {
                var fmt = new LuaFloatFormatProvider();
                lines.Add(
                    String.Format(
                    "        pylon({0}, {1}, {2}, {3}, {4},", p.Index,
                        p.Coordinates[0].ToString(fmt),
                        p.Coordinates[1].ToString("0.000000", fmt), // 6 numbers after point
                        p.Coordinates[2].ToString("0.000000", fmt),
                        p.Coordinates[3].ToString("0.000000", fmt))
                    );
                lines.Add(
                    "            {");

                if (p.Fiz != null)
                {
                    lines.Add(
                        String.Format(
                        "                FiZ = {0},", p.Fiz));
                }
                if (p.Absent != null && p.AbsentValue != null)
                {
                    lines.Add(
                        String.Format(
                        "                absent = {0},", p.AbsentValue));
                }
                lines.Add(
                    "            },");
                lines.Add(
                    "            {");
                //weapons
                foreach (var w in p.Weapons)
                {
                    //                { CLSID = "{7575BA0B-7294-4844-857B-031A144B2595}" },
                    lines.Add(
                        "                { CLSID = \"{" + w.ID.ToString().ToUpper() + "}\" },"
                        );
                }

                lines.Add(
                    "            }");
                lines.Add("        ),");

            }
            lines.Add("    },");

            return lines.ToArray();
        }

        public string[] SerializeTasks(Aircraft aircraft)
        {
            var lines = new List<string>();

            lines.Add("    {");

            foreach (var t in aircraft.Tasks)
            {
                lines.Add(
                    String.Format("        aircraft_task({0}),", t)
                    );
            }

            lines.Add("    },");

            if (!String.IsNullOrEmpty(aircraft.DefaultTask))
            {
                lines.Add(
                        String.Format("	aircraft_task({0})", aircraft.DefaultTask)
                        );
            }

            lines.Add(");");

            return lines.ToArray();
        }
    }
}
